package cn.fintecher.pangolin.service.dataimp.task;

import cn.fintecher.pangolin.common.enums.ImportDataExcelStatus;
import cn.fintecher.pangolin.common.model.UserModel;
import cn.fintecher.pangolin.common.utils.ZWDateUtil;
import cn.fintecher.pangolin.entity.elastic.DataImportRecord;
import cn.fintecher.pangolin.entity.elastic.PreRecord;
import cn.fintecher.pangolin.entity.elastic.PreRecordModel;
import cn.fintecher.pangolin.service.dataimp.elastic.DataImportRecordElastic;
import cn.fintecher.pangolin.service.dataimp.elastic.PreRecordElastic;
import cn.fintecher.pangolin.service.dataimp.elastic.PreRecordModelElastic;
import cn.fintecher.pangolin.common.utils.RepositoryUtil;
import org.apache.commons.collections4.IterableUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Auther: xiaqun
 * @Description:
 * @Date: 16:39 2019/11/5
 */

@Service("cancelPreTask")
public class CancelPreTask {
    @Autowired
    private PreRecordModelElastic preRecordModelElastic;

    @Autowired
    private PreRecordElastic preRecordElastic;

    @Autowired
    private DataImportRecordElastic dataImportRecordElastic;

    /**
     * 撤销委前催记导入
     */
    @Async
    @Transactional
    public void cancelPre(BoolQueryBuilder builder, DataImportRecord dataImportRecord, UserModel userModel) {
        Iterable<PreRecordModel> preRecordModels = preRecordModelElastic.search(builder);
        Iterable<PreRecord> preRecords = preRecordElastic.search(builder);
        preRecordModels.forEach(e -> e.setFlag(1));
        preRecords.forEach(e -> e.setFlag(1));
        RepositoryUtil.saveData(preRecordModelElastic, IterableUtils.toList(preRecordModels));
        RepositoryUtil.saveData(preRecordElastic, IterableUtils.toList(preRecords));
        dataImportRecord.setStatus(ImportDataExcelStatus.BILL_REVOKE);
        dataImportRecord.process(userModel.getRealName(), ZWDateUtil.getNowDateTime());
        dataImportRecordElastic.save(dataImportRecord);
    }
}
